package com.woniuxy.contract.service.impl;

import com.woniuxy.commons.entity.ContractLog;
import com.woniuxy.commons.utils.GetIp;
import com.woniuxy.contract.mapper.ContractLogMapper;
import com.woniuxy.contract.service.ContractLogService;
import org.aspectj.lang.JoinPoint;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2021-11-13 14:54
 */
@Service
public class ContractLogServiceImpl implements ContractLogService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;



    @Resource
    private ContractLogMapper contractLogMapper;


    @Override
    public void addContractLog(JoinPoint joinPoint, String methodName, String module, String description) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = GetIp.getIpAddress(request);
        String username = (String) redisTemplate.opsForValue().get("username");
        ContractLog contractLog =new ContractLog();
         contractLog.setUsername(username);
         contractLog.setOperatortime(new Date());
         contractLog.setContent(description);
         contractLog.setHostip(ip);
         contractLogMapper.addContractLog(contractLog);

    }

    @Override
    public List<ContractLog> getAllContractLog(Integer pageIndex, Integer pageSize) {
        List<ContractLog> allContractLog = contractLogMapper.getAllContractLog(pageIndex, pageSize);
        return allContractLog;
    }

    @Override
    public void delContractLog(int logid) {
        contractLogMapper.delContractLog(logid);
    }

    @Override
    public int selectContractLogCount() {
        return contractLogMapper.selectContractLogCount();
    }
}
